package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.CollectShop;
import com.tjetc.entity.Merchant;
import com.tjetc.service.CollectShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("collectShop")
public class CollectShopController {
    @Autowired
    private CollectShopService collectShopService;

    /***
     * 添加用户收藏的店铺
     * @param collectShop
     * @return
     */
    @RequestMapping("add")
    private JsonResult addCollectShop(CollectShop collectShop){
        int row = collectShopService.addShop(collectShop);
        JsonResult result = new JsonResult<>(0, "", row);
        return result;
    }

    /***
     * 删除用户收藏的店铺
     * @param userId
     * @param merchantId
     * @return
     */
    @RequestMapping("delete")
    private JsonResult deleteCollectShop(Integer userId,Integer merchantId){
        int row = collectShopService.deleteShop(userId,merchantId);
        if(row>0){
            return new JsonResult(0,"删除成功",row);
        }else{
            return new JsonResult(1,"删除失败",row);
        }
    }

    /***
     * 获取用户收藏的所有店铺
     * @param userId
     * @return
     */
    @RequestMapping("all/{id}")
    private JsonResult getAll(@PathVariable("id") Integer userId){
        List<Merchant> shops = collectShopService.selectByUserId(userId);
        if(shops!=null){
            return new JsonResult(0, "查询成功", shops);
        }else{
            return new JsonResult(0, "查询失败，不存在这样的收藏店铺", shops);
        }
    }

    /***
     * 查询该店铺是否被该用户收藏
     * @param userId
     * @param merchantId
     * @return
     */
    @RequestMapping("query/{userId}/{merchantId}")
    private JsonResult queryCollect(@PathVariable Integer userId,@PathVariable Integer merchantId){
        CollectShop collectShop = collectShopService.queryCollect(userId, merchantId);
        if(collectShop!=null){
            return new JsonResult(0,"在收藏中",collectShop.getId());
        }else{
            return new JsonResult(0,"不在收藏中",0);
        }
    }

    /***
     * 获取店铺人均消费金额
     * @param id
     * @return
     */
    @RequestMapping("getamount/{id}")
    private JsonResult getPeronlyAmount(@PathVariable Integer id){
        BigDecimal monthlyOrderAmount = collectShopService.getMonthlyOrderAmount(id);
        if(monthlyOrderAmount!=null){
            return new JsonResult(0,"",monthlyOrderAmount);
        }else{
            monthlyOrderAmount=new BigDecimal(0);
            return new JsonResult(0,"",monthlyOrderAmount);
        }
    }

    /***
     * 获取月销售量
     * 当天起算往前推一个月
     * @param id
     * @return
     */
    @RequestMapping("getnum/{id}")
    private JsonResult getMonthlyNum(@PathVariable Integer id){
        Integer monthlyOrderNums = collectShopService.getMonthlyOrderNums(id);
        if(monthlyOrderNums>0){
            return new JsonResult(0,"",monthlyOrderNums);
        }else{
            return new JsonResult(0,"",0);
        }
    }

}
